import counter
from google.appengine.api import memcache
from shard import *

class CachedCounter(counter.Counter):
    def get(self):
        """Retrieve the value for a given sharded counter."""
        key=self.__class__.__name__
        cnt=memcache.get(key)
        if cnt is not None:
            return cnt
        #else:
        
        total = 0
        for counter in self.Shard.all():
            total += counter.count
        memcache.add(key,total,600)
        return total

class CachedUserCounter(CachedCounter):
    NUM_SHARDS  =2
    Shard       =UserCounterShard
    
def main():
    cuc=CachedUserCounter()
    print cuc.get()

if __name__ == '__main__':
    main()      